package com.yisu.expressway.manager.website.interceptor;


import com.yisu.expressway.manager.domain.util.Constants;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2016/4/27.
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {
    private static final String[] EXCLUDE_URI = {"/", "/index.htm", "/user/login.json"};

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestUri = request.getRequestURI();
        for(String uri : EXCLUDE_URI) {
            if(requestUri.equalsIgnoreCase(uri)) {
                return true;
            }
        }

        if(request.getSession().getAttribute(Constants.USER_INFO_KEY) == null) {
            response.sendRedirect(request.getContextPath() + "/index.htm");
            return false;
        }

//        response.setHeader("Access-Control-Allow-Origin", "*");

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }

}